package com.linfnn.demo.Controller;

import com.baomidou.mybatisplus.annotation.TableId;
import com.linfnn.demo.Model.Assessment;
import com.linfnn.demo.Model.Disease;
import com.linfnn.demo.Service.impl.AssessmentServiceImpl;
import com.linfnn.demo.Service.impl.DiseaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Controller
public class FunctionDisease {

        @Autowired
        private DiseaseServiceImpl diseaseService ;

        //  这是管理页面，同时可以刷新数据，进行操作后跳到这里会重新获取数据，从而刷新页面
        @RequestMapping(value = "/manage_disease")
        public ModelAndView showAll() {
            List<Disease> diseases=diseaseService.list();
            //        返回的页面
            ModelAndView mav = new ModelAndView("/managehtml/diseaseManage.html");
            //        页面和对应的数据结合
            mav.addObject("results",diseases);
            return mav;
        }
    @PostMapping("/search/disease")
    public ModelAndView search(@RequestParam("diseaseCode") String diseaseCode) {
            Disease disease=diseaseService.getById(diseaseCode);
        ModelAndView mav = new ModelAndView("/search/index5.html");
        mav.addObject("results", disease);
        return mav;
    }


    //    注意这里返回应该更换为html，不能再去直接刷新获取整个数据库的数据， 并且这里的路由为searchadmin，前面有一个search，两者不一样
    @PostMapping("/searchadmin/disease")
    public ModelAndView searchadmin(@RequestParam("diseaseCode") String diseaseCode) {
        Disease disease=diseaseService.getById(diseaseCode);
     //   Assessment assessment = assessmentService.getAssessmentByName(assessmentName);
        ModelAndView mav = new ModelAndView("/managehtml/diseaseManage.html");
        mav.addObject("results", disease);
        return mav;
    }

    @RequestMapping("/delete/disease")
    public ModelAndView deleteDisease(@RequestParam("diseaseCode") String diseaseCode) {
        // Delete assessment logic here
        if (diseaseCode.isEmpty()){
            System.out.println("delete输入为空");
        }
        if(diseaseService.removeById(diseaseCode)){
            System.out.println("删除成功");
        };

        ModelAndView mav = new ModelAndView("redirect:/manage_disease");
        return mav;
    }



    //    这里点击更新后，会跳转到xxxUpdata.html文件里面去，然后再执行更新
    @RequestMapping("/updatehtml/disease")
    public ModelAndView updateDiseasehtml() {
        return new ModelAndView("redirect:/updatahtml/diseaseUpdata.html");
    }

    @RequestMapping("/update/disease")
    public ModelAndView updateDisease(@RequestParam("diseaseCode") String diseaseCode,
                                      @RequestParam("diseaseName") String diseaseName,
                                      @RequestParam("symptoms") String symptoms,
                                      @RequestParam("possibleCauses") String possibleCauses,
                                      @RequestParam("commonPopulation") String commonPopulation,
                                      @RequestParam("preventionMeasures") String preventionMeasures,
                                      @RequestParam("treatmentMethods") String treatmentMethods,
                                      @RequestParam("treatmentPeriod") String treatmentPeriod,
                                      @RequestParam("remarks") String remarks){
        // Update assessment logic here
//    这里逻辑比较复杂，为了方便，这里使用的是先删除对应数据，然后插入该数据的操作
        if(diseaseService.removeById(diseaseCode)){
            System.out.println("更新——删除成功");
        };

        Disease disease=new Disease();
        disease.setDiseaseCode(diseaseCode);
        disease.setDiseaseName(diseaseName);
        disease.setSymptoms(symptoms);
        disease.setPossibleCauses(possibleCauses);
        disease.setCommonPopulation(commonPopulation);
        disease.setPreventionMeasures(preventionMeasures);
        disease.setTreatmentMethods(treatmentMethods);
        disease.setTreatmentPeriod(treatmentPeriod);
        disease.setRemarks(remarks);

//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        String currentDateTime = LocalDateTime.now().format(formatter);
//        disease.setCreationTime(currentDateTime);


        if(diseaseService.save(disease)){
            System.out.println("更新——添加成功");
        };

        return new ModelAndView("redirect:/manage_disease");
    }



    @RequestMapping("/add/disease")
    public ModelAndView disease(@RequestParam("diseaseCode") String diseaseCode,
                                @RequestParam("diseaseName") String diseaseName,
                                @RequestParam("symptoms") String symptoms,
                               @RequestParam("possibleCauses") String possibleCauses,
                                @RequestParam("commonPopulation") String commonPopulation,

                                @RequestParam("preventionMeasures") String preventionMeasures,
                                @RequestParam("treatmentMethods") String treatmentMethods,
                                @RequestParam("treatmentPeriod") String treatmentPeriod,
                                @RequestParam("remarks") String remarks) {
        // Add assessment logic here
        Disease disease=new Disease();
        disease.setDiseaseCode(diseaseCode);
        disease.setDiseaseName(diseaseName);
        disease.setSymptoms(symptoms);
        disease.setPossibleCauses(possibleCauses);
       disease.setCommonPopulation(commonPopulation);
        disease.setPreventionMeasures(preventionMeasures);
        disease.setTreatmentMethods(treatmentMethods);
        disease.setTreatmentPeriod(treatmentPeriod);
        disease.setRemarks(remarks);


        if(diseaseService.save(disease)){
            System.out.println("添加成功");
        };
        return new ModelAndView("redirect:/manage_disease");
    }






}

